/*
 * Copyright 2012-2013 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.list;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.Scheduler;
import finbudget.client.lists.GetListOfItems;
import finbudget.client.lists.ListArrivedEvent;
import finbudget.client.lists.ListArrivedHandler;
import finbudget.client.lists.ListsManager;
import finbudget.client.ui.table.Record;
import finbudgettable.client.ui.grid.CellGrid;
import finbudgettable.client.ui.grid.cell.CommonComparator;
import finbudgettable.client.ui.grid.column.ColumnMenu;
import finbudgettable.client.ui.grid.column.ColumnMenuImpl;
import finbudgettable.client.ui.grid.column.GridColumn;

import java.util.Comparator;
import java.util.List;

import static finbudget.client.lists.GetListOfItems.ListType;

public class ListColumn extends GridColumn<String> {

    public ListColumn(Builder builder) {
        super(builder);
        initFieldUpdater(builder.getFieldName());
    }

    @Override
    protected boolean isValueChanged(Record record, String value) {
        return CommonComparator.isNotEqual(getValue(record), value);
    }

    @Override
    protected String getCellStyleNames() {
        return finbudgettable.client.ui.grid.cell.list.ListCell.RESOURCES.style().bListCell();
    }

    private void initFieldUpdater(final String fieldName) {
        setFieldUpdater(new FieldUpdater<Record, String>() {
            public void update(int index, Record record, String value) {
                record.setValue(fieldName, value != null && !value.isEmpty() ? value : null);
            }
        });
    }

    @Override
    public String getValue(Record record) {
        return record.getValueString(getFieldName());
    }

    public static class Builder extends GridColumn.Builder<ListCell, String> {

        private final ListsManager listsManager;
        private final ListType listType;

        Builder(CellGrid grid, String fieldName, ListsManager listsManager, ListType listType, boolean hasTotals) {
            super(grid, fieldName, hasTotals);
            this.listsManager = listsManager;
            this.listType = listType;
        }

        @Override
        protected ListCell createCell() {
            List<GetListOfItems.BudgetListItem> list = listsManager.getList(listType);
            if (list.isEmpty()) {
                listsManager.addListArrivedEventHandler(new ListArrivedHandler() {
                    @Override
                    public void onListArrived(ListArrivedEvent event) {
                        if (event.getListType().equals(listType)) {
                            listsManager.removeListArrivedEventHandler(this);
                            Scheduler.get().scheduleFinally(
                                    new Scheduler.ScheduledCommand() {
                                        @Override
                                        public void execute() {
                                            grid.refreshColumn(fieldName);
                                        }
                                    }
                            );
                        }
                    }
                });
            }
            listsManager.requestList(listType);
            return new ListCell(nullable, list);
        }

        @Override
        protected Comparator<Record> getDefaultComparator() {
            return new ListComparator(fieldName, listsManager.getList(listType));
        }

        public ColumnMenu getDefaultColumnMenu() {
            final ColumnMenu columnMenu = new ColumnMenuImpl(grid, new ListFilterPanel(fieldName, listsManager, listType));
            setColumnMenu(columnMenu);
            return columnMenu;
        }

        @Override
        protected GridColumn<String> createColumn(Cell<String> cell) {
            return new ListColumn(this);
        }
    }

    public static Builder builder(CellGrid grid, String fieldName, ListsManager listsManager, ListType listType, boolean hasTotals) {
        return new Builder(grid, fieldName, listsManager, listType, hasTotals);
    }

}
